//
//  SWOrder.swift
//  ChildStory
//
//  Created by linhan on 2017/1/14.
//  Copyright © 2017年 Aiya. All rights reserved.
//

import Foundation

//产品套装(简装、精装)
class SWProductPackage: NSObject
{
    var id:String = ""
    var title:String = ""
    var detailURL:String = ""
    var price:Double = 0
    
}

//付款请求单(凭借payID获取微信、支付宝支付参数)
struct SWPurchaseRequisition
{
    var orderID:String
    var payID:String
    var price:Double = 0
    var productID:Int = 0
    
    init(orderID:String, payID:String)
    {
        self.orderID = orderID
        self.payID = payID
    }
    
    init?(response:SWPurchaseRequisitionRsp)
    {
        guard let orderID = response.orderID,
              let payID = response.payID,orderID != "" && payID != "" else{
            return nil
        }
        self.orderID = orderID
        self.payID = payID
        price = response.price ?? price
        productID = response.productID ?? productID
    }
}

extension SWOrderStatus
{
    var index:Int{
        switch self {
        case .unknow: return -1
        case .unpaid: return 0
        case .purchased: return 1
        case .uploaded: return 2
        case .making: return 3
        case .shipped: return 4
        case .delivered: return 5
        }
    }
    
    var name:String{
        switch self {
        case .unknow: return "未知"
        case .unpaid: return "未付款"
        case .purchased: return "已付款"
        case .uploaded: return "已上传"
        case .making: return "生产中"
        case .shipped: return "已寄送"
        case .delivered: return "已签收"
        }
    }
}

extension SWOrder
{
    convenience init?(response:SWOrderRsp)
    {
        self.init()
        guard let orderID = response.orderID,orderID != "" else{
            return nil
        }
        self.orderID = orderID
        //payID = response.payID
        remark = response.remark
        creationDate = response.creationDate
        price = response.price ?? 0.0
        remark = response.remark
        
        if let orderResponse = response.addressee
        {
            addresseeInfo = SWAddresseeInfo(orderResponse: orderResponse)
        }
        
        status = SWOrderStatus(rawValue: response.status ?? 0) ?? SWOrderStatus.unpaid
        
        products = response.products?.map({SWProduct(response: $0)})
        
    }
    
}

extension SWProduct
{
    convenience init(response:SWOrderProductRsp)
    {
        self.init()
        
        productID = response.id
        title = response.title
        price = response.price ?? 0.0
        count = response.count ?? 0
    }
}

//收货省市区
struct SWRegionItem
{
    var title:String = ""
    var code:Int = 0
    
    init(title:String, code:Int)
    {
        self.title = title
        self.code = code
    }
    
    init?(response:SWProvinceRsp)
    {
        //super.init()
        guard let code = response.code else{
            return nil
        }
        title = response.title ?? ""
        self.code = code
    }
    
    init?(response:SWCityRsp)
    {
        guard let code = response.code else{
            return nil
        }
        title = response.title ?? ""
        self.code = code
    }
    
    init?(response:SWDistrictRsp)
    {
        guard let code = response.code else{
            return nil
        }
        title = response.title ?? ""
        self.code = code
    }
}


//收件人信息
extension SWAddresseeInfo
{
    
    convenience init?(response:SWAddresseeInfoRsp)
    {
        self.init()
        
        guard let id = response.id,let receiver = response.receiver,
            let provinceCode = response.provinceCode,
            let cityCode = response.cityCode,
            let districtCode = response.districtCode,
            let block = response.block,
            receiver != "" && provinceCode > 0 && cityCode > 0 && districtCode > 0 && block != "" else{
                return nil
        }
        self.addressID = id
        self.receiver = receiver
        self.provinceCode = provinceCode
        self.cityCode = cityCode
        self.block = block
        provinceName = response.provinceName
        cityName = response.cityName
        districtName = response.districtName
        phone = response.phone
        postcode = response.postcode
    }
    
    convenience init(orderResponse:SWOrderAddresseeInfoRsp)
    {
        self.init()
        
        let response = orderResponse
        
        addressID = response.id ?? 0
        receiver = response.receiver
        phone = response.phone
        provinceName = response.provinceName
        cityName = response.cityName
        districtName = response.districtName
        block = response.block
        postcode = response.postcode
    }
    
    //地址是否有效
    var valid:Bool{
        if addressID > 0
        {
            return true
        }
        return receiver != "" && provinceCode > 0 && cityCode > 0 && districtCode > 0 && block != ""
    }
}


